package ONLINE_EXAM;

import java.util.Stack;

public class hwOnline {
    public static void main(String[] args) {
        hwOnline test = new hwOnline();
        String input = "()[{}";
        System.out.println(test.isValid(input));

    }

    /**
     * 栈
     * @param s
     * @return
     */
    public static boolean isValid(String s){
        Stack<Character> stack = new Stack<>();

        for(char ch :s.toCharArray()){
            if (ch == '(' || ch == '{'|| ch == '[') {
                stack.push(ch);
            }
            if (ch == ')'){

                if (stack.isEmpty() ){
                    return false;
                }else if (stack.peek()=='('){
                    stack.pop();
                    continue;
                }else {
                    stack.push(ch);
                }
            }
            if (ch == '}'){

                if (stack.isEmpty() ){
                    return false;
                }else if (stack.peek()=='{'){
                    stack.pop();
                    continue;
                }else {
                    stack.push(ch);
                }
            }
            if (ch == ']'){

                if (stack.isEmpty() ){
                    return false;
                }else if (stack.peek()=='['){
                    stack.pop();
                    continue;
                }else {
                    stack.push(ch);
                }
            }

        }


        if (!stack.isEmpty()){
            return false;
        }else {
            return true;
        }

    }
}
